package br.ufpr.tads.Rent.model;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author NI LINDA
 */
import br.ufpr.tads.Rent.controller.EnumClass.categoria;
import br.ufpr.tads.Rent.controller.EnumClass.marca;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class LocacaoDAO {
    private Connection cn;
    private static final String sqlselectveiculo = "select * from veiculo where estado = 2 AND categoria = (?)";
    private static final String sqlselectcliente = "select * from cliente where  nome = (?) OR sobrenome = (?) OR cpf = (?);";
    private static final String sqlinsert = "insert into locacao (idCliente,idVeiculo,numDiasLoc,dtLoc)values(?,?,?,?)";
    private static final String sqldevolve = "alter table locacao (idCliente,idVeiculo,numDiasLoc,dtLoc)values(?,?,?,?)";
   
    public LocacaoDAO() throws FileNotFoundException, ClassNotFoundException, IOException, SQLException {
        this.cn =  new ConnectionFactory().getConnection();
    }
    public List<ClienteDTO> SelecionaCliente (String nome)throws SQLException{
        PreparedStatement stmt = cn.prepareStatement(sqlselectcliente);
        stmt.setString(1, nome);
        stmt.setString(2, nome);
        stmt.setString(3, nome);
        List<ClienteDTO> lista = new ArrayList ();
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            ClienteDTO ct = new ClienteDTO();
            ct.setIdCliente(rs.getInt("idcliente"));
            ct.setNome(rs.getString("nome"));
            ct.setSobrenome(rs.getString("sobrenome"));
            ct.setEndereco(rs.getString("endereco"));
            ct.setRg(rs.getString("rg"));
            ct.setCpf(rs.getString("cpf"));
            lista.add(ct);
        }
        rs.close();
        stmt.close();
        cn.close();
        return lista;
    }
    
    public List<VeiculoDTO> CarregaVeiculos (int cat)throws SQLException{
        PreparedStatement stmt = cn.prepareStatement(sqlselectveiculo);
        stmt.setInt(1, cat);
        ResultSet rs = stmt.executeQuery();
        List<VeiculoDTO> lista = new ArrayList();
        while(rs.next()){
            VeiculoDTO ct = new VeiculoDTO();
            ct.setId(rs.getInt("idveiculo"));
            ct.setMarca(marca.values()[rs.getInt("marca")]);
            ct.setCategoria(categoria.values()[rs.getInt("categoria")]);
            ct.setAno(rs.getInt("ano"));
            ct.setModelo(rs.getInt("modelo"));
            lista.add(ct);
        }
        rs.close();
        stmt.close();
        cn.close();
        return lista;
    }
   
    public void Locar (LocacaoDTO loc) {
        PreparedStatement stmt;
        try {
            stmt = cn.prepareStatement(sqlinsert);
       
        stmt.setInt(1, loc.getCliente());
        stmt.setInt(2,loc.getVeiculo() );
        stmt.setInt(3, loc.getDiasLoc());
        stmt.setDate(4, new java.sql.Date(loc.getData().getTimeInMillis()));
        stmt.executeUpdate();
        JOptionPane.showMessageDialog(null,"Veiculo Locado com Seucesso", "Erro", JOptionPane.ERROR_MESSAGE);
        
        stmt.close();
         } catch (SQLException ex) {
            Logger.getLogger(LocacaoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
